package com.qianzhou.eduCourseBoot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianzhou.eduCommon.dto.CourseDTO;
import com.qianzhou.eduCommon.dto.CoursePageQueryDTO;
import com.qianzhou.eduCommon.mapper.CourseMapper;
import com.qianzhou.eduCommon.mapper.TeacherMapper;
import com.qianzhou.eduCommon.po.Course;
import com.qianzhou.eduCommon.po.Teacher;
import com.qianzhou.eduCommon.utils.PageInfoQuery;
import com.qianzhou.eduCommon.vo.CourseVO;
import com.qianzhou.eduCommon.vo.PageInfoVO;
import com.qianzhou.eduCourseBoot.converter.CourseConverter;
import com.qianzhou.eduCourseBoot.converter.TeacherConverter;
import com.qianzhou.eduCourseBoot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    /**
     * @Author: qian.zhou
     * @Description: 根据搜索条件查询课程信息
     * @DateTime: 2021/10/10 17:32
     * @Params: * @Param dto:
     * @Return: * @return: PageInfoVO<Course>
     */
    @Override
    public PageInfoVO<Course> findCourseByCondition(CoursePageQueryDTO dto) {
        PageInfoVO<Course> pageInfoVO;
        PageInfoQuery<Course> pageInfoQuery = new PageInfoQuery<>();
        IPage<Course> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (dto.getCourseName() != null && !Objects.equals(dto.getCourseName(), "")) {
            queryWrapper.eq("course_name", dto.getCourseName());
        }
        if (dto.getStatus() != null) {
            queryWrapper.eq("status", dto.getStatus());
        }
        courseMapper.selectPage(page, queryWrapper);
        pageInfoVO = pageInfoQuery.getPageInfoData(page);
        return pageInfoVO;
    }

    /**
     * @Author: qian.zhou
     * @Description: 新建课程
     * @DateTime: 2021/10/10 17:40
     * @Params: * @Param dto:
     * @Return: * @return: Integer
     */
    @Override
    public Integer saveCourseOrTeacher(CourseDTO dto) {
        Course course = CourseConverter.INSTANCE.dtoConvertPo(dto);
        Date date = new Date();
        course.setCreateTime(date);
        course.setUpdateTime(date);
        int num1 = courseMapper.insert(course);
        Teacher teacher = TeacherConverter.INSTANCE.dtoConvertPo(dto);
        teacher.setCreateTime(date);
        teacher.setUpdateTime(date);
        int num2 = teacherMapper.insert(teacher);
        return num1 + num2;
    }

    /**
     * @Author: qian.zhou
     * @Description: 编辑课程
     * @DateTime: 2021/10/10 17:40
     * @Params: * @Param dto:
     * @Return: * @return: Integer
     */
    @Override
    public Integer updateCourseOrTeacher(CourseDTO dto) {
        Course course = CourseConverter.INSTANCE.dtoConvertPo(dto);
        Teacher teacher = TeacherConverter.INSTANCE.dtoConvertPo(dto);
        Date date = new Date();
        course.setUpdateTime(date);
        teacher.setUpdateTime(date);
        int num1 = courseMapper.updateById(course);
        int num2 = teacherMapper.updateById(teacher);
        return num1 + num2;
    }

    /**
     * @Author: qian.zhou
     * @Description:
     * @DateTime: 2021/10/10 19:05
     * @Params: * @Param dto:
     * @Return: * @return: Integer
     */
    @Override
    public Integer updateCourseStatus(CourseDTO dto) {
        Course course = CourseConverter.INSTANCE.dtoConvertPo(dto);
        course.setUpdateTime(new Date());
        return courseMapper.updateById(course);
    }
    /**
     * @Author: qian.zhou
     * @Description: 编辑时的回显操作 其实就是简单的回显
     * @DateTime: 2021/10/10 19:15
     * @Params: * @Param id:
     * @Return: * @return: CourseVO
     */
    @Override
    public CourseVO findCourseById(Integer id) {
        Course course = courseMapper.selectById(id);
        return CourseConverter.INSTANCE.poConvertVo(course);
    }
}
